package com.example.bob.controller;


import com.example.bob.common.AppVariable;
import com.example.bob.common.PasswordUtils;
import com.example.bob.common.ResultAjax;
import com.example.bob.model.UserInfo;
import com.example.bob.model.vo.UserInfoVO;
import com.example.bob.service.UserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@ResponseBody
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;


    //注册
    @RequestMapping("/reg")
    public ResultAjax regUserInfo(UserInfo userInfo){
        userInfo.setPassword(PasswordUtils.encrypt(userInfo.getPassword()));
        return ResultAjax.succ(userService.regUserInfo(userInfo));
    }

    //登录
    @RequestMapping("/login")
    //登录：前端会向后端传递用户名和密码，使用扩展对象接受！
    public ResultAjax login(UserInfoVO userInfoVO, HttpServletRequest request){

        UserInfo userInfo = userService.getUserByName(userInfoVO.getUsername());
        // 先判断能否查询到用户！
        if(userInfo==null||userInfo.getId()==0){
            return ResultAjax.fail(-1,"用户名或密码错误！");
        }
        // 在判断密码是否正确
//        userInfoVO.setPassword(PasswordUtils.encrypt(userInfoVO.getPassword()));
        if(!PasswordUtils.decrypt(userInfoVO.getPassword(),userInfo.getPassword())){
            return ResultAjax.fail(-1,"用户名或密码错误！");
        }

        HttpSession session = request.getSession();//默认为true，有就获取，没有就创建

        //说明 注销功能没有问题！
        /*
        * if(session.getAttribute(AppVariable.SESSION_USERINFO_KEY)==null)
            System.out.println("SESSION_USERINFO_KEY not exits");
        else
            System.out.println("SESSION_USERINFO_KEY exits");

        * */

        //为什么要设计这个呀！ SESSION_USERINFO_KEY  只是一个名称罢了！
        session.setAttribute(AppVariable.SESSION_USERINFO_KEY, userInfo);
        return ResultAjax.succ(true);
    }
    //注销
    @RequestMapping("/logout")
    public int logOut(HttpServletRequest request){
        HttpSession session = request.getSession(false);
        if (session != null && session.getAttribute(AppVariable.SESSION_USERINFO_KEY) != null) {
            session.removeAttribute(AppVariable.SESSION_USERINFO_KEY);
        }

        if(session.getAttribute(AppVariable.SESSION_USERINFO_KEY)==null)
            System.out.println("session==null");
        return 1;
    }

    @RequestMapping("/checkSession")
    public ResultAjax checkSession(HttpServletRequest request){
        HttpSession session = request.getSession(false);
        if (session != null && session.getAttribute(AppVariable.SESSION_USERINFO_KEY) != null) {
            //登陆成功
            return ResultAjax.succ(true);
        }
        System.out.println(request.getRequestURL());

        return ResultAjax.fail(401,"未登录");
    }



}
